/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pa165.dominatingspecies.frontend.element;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.model.StringResourceModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.springframework.dao.DataAccessException;
import org.springframework.security.access.prepost.PreAuthorize;
import pa165.dominating.species.enums.ElementType;
import pa165.dominating.species.service.ElementService;
import pa165.dominating.species.tos.ElementTo;

import pa165.dominatingspecies.frontend.pages.BasePage;

/**
 *
 * @author Ondrej Prazak
 */
@PreAuthorize("hasRole('WORKER')")
public class EditElementPage extends BasePage {

    @SpringBean(name = "elementService")
    private ElementService elementService;
    private ElementTo ele;

    EditElementPage(Long id) {
        if (id != null) {
            ele = elementService.getElementToById(id);
        }
        Form<ElementTo> form = new Form<>("form");
        form.setModel(new CompoundPropertyModel<>(ele));
        form.add(new Label("desc", new StringResourceModel("desc", this, null)));
        form.add(new TextArea<String>("description"));
        form.add(new Label("eleType", new StringResourceModel("chooseType", this, null)));

        form.add(new EnumDropDown<>("types", new PropertyModel<ElementType>(ele, "type")));

        Button submitButton = new Button("submitButton") {
            @Override
            public void onSubmit() {

                if (ele.getType() == null) {
                    getSession().warn(new StringResourceModel("eleNotSelected", this, null).getString());
                    setResponsePage(new EditElementPage(ele.getId()));
                } else {
                    boolean thrown = false;
                    try {
                        elementService.updateElementTo(ele);
                    } catch (DataAccessException ex) {
                        getSession().warn(new StringResourceModel("eleExists", this, null, new Object[]{ele.getId()}).getString());
                        setResponsePage(new EditElementPage(ele.getId()));
                        thrown = true;
                    }
                    if (!thrown) {
                        getSession().info(new StringResourceModel("updateSucc", this, null, new Object[]{ele.getId()}).getString());
                        setResponsePage(ElementsPage.class);
                    }
                }
            }
        };
        form.add(submitButton);

        Link<BasePage> goBack = new Link<BasePage>("goBack") {
            @Override
            public void onClick() {
                setResponsePage(new ElementsPage());
            }
        };
        form.add(goBack);
        add(form);
    }
}
